import Setting from "../../../components/dialog/data-source-dialog/setting";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheet, sheetIndex, sheetName, selections, mainTableObj,
            getDetailIndex, getFieldIndex, getFieldCellType, getFieldName, includeTitle,nodetailJson } = options;
        sheet.suspendPaint();
        try {
            if (isUndo) {
            selections.forEach(selection => {
                const { row, col, rowCount, colCount } = selection;
                for (let currentRow = row, i = 0; i < rowCount; i++ , currentRow++) {
                    for (let currentCol = col, j = 0; j < colCount; currentCol++ , j++) {
                        sheet.getCell(currentRow, currentCol)
                            .text(options._oldCellText[currentRow][currentCol])
                            .backColor(options._oldBackColor[currentRow][currentCol])
                            .textIndent(options._oldTextIndent[currentRow][currentCol])
                            .backgroundImageLayout(options._oldBackgroundImageLayout[currentRow][currentCol])
                            .backgroundImage(options._oldBackGroundImage[currentRow][currentCol]);
                    }
                }
            });
        } else {
                const mainTable = `mainTable_${sheetIndex}`;
                options._oldBackColor = {};
                options._oldCellText = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                options._oldTag = {};
                options._oldLocked = {};
                selections.forEach(selection => {
                    const { row, col, rowCount, colCount } = selection;
                    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                        const currentRow = row + rowIndex;
                        if (!options._oldCellText[currentRow]) {
                            options._oldCellText[currentRow] = {};
                        }
                        if (!options._oldBackColor[currentRow]) {
                            options._oldBackColor[currentRow] = {}
                        }
                        if (!options._oldCellTypes[currentRow]) {
                            options._oldCellTypes[currentRow] = {};
                        }
                        if (!options._oldTextIndent[currentRow]) {
                            options._oldTextIndent[currentRow] = {};
                        }
                        if (!options._oldBackGroundImage[currentRow]) {
                            options._oldBackGroundImage[currentRow] = {};
                        }
                        if (!options._oldBackgroundImageLayout[currentRow]) {
                            options._oldBackgroundImageLayout[currentRow] = {};
                        }
                        if (!options._oldLocked[currentRow]) {
                            options._oldLocked[currentRow] = {};
                        }
                        for (let colIndex = 0; colIndex < colCount; colIndex++) {
                            const currentCol = col + colIndex;
                            const cell = sheet.getCell(currentRow, currentCol);
                            options._oldBackColor[currentRow][currentCol] = cell.backColor();
                            options._oldCellText[currentRow][currentCol] = cell.text();
                            options._oldTextIndent[currentRow][currentCol] = cell.textIndent();
                            options._oldBackGroundImage[currentRow][currentCol] = cell.backgroundImage();
                            options._oldBackgroundImageLayout[currentRow][currentCol] = cell.backgroundImageLayout();
                        }
                    }
                    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                        const currentRow = row + rowIndex;
                        for (let colIndex = 0; colIndex < colCount; colIndex++) {
                            const currentCol = col + colIndex;
                            if (sheet.tables.find(currentRow, currentCol)) {
                                return;
                            }
                        }
                    }

                    let sheetNodetailMap = new Map();
                    if(nodetailJson.has(sheetName)) {
                        sheetNodetailMap = nodetailJson.get(sheetName);
                    }

                    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
                        const currentRow = row + rowIndex;
                        let rowSet = new Set();
                        if(sheetNodetailMap.has(currentRow)){
                            rowSet = sheetNodetailMap.get(currentRow);
                        }
                        for (let colIndex = 0; colIndex < colCount; colIndex++) {
                            const currentCol = col + colIndex;
                            rowSet.add(currentCol);
                            }
                            sheetNodetailMap.set(currentRow,rowSet);
                    }
                    nodetailJson.set(sheetName,sheetNodetailMap);
                    sheet.getRange(row, col, rowCount, colCount).backColor("yellow");
                });
                
            }
        
            // if (isUndo) {
            //     selections.forEach(selection => {
            //         const { row, col, rowCount, colCount } = selection;
            //         for (let currentRow = row, i = 0; i < rowCount; i++ , currentRow++) {
            //             for (let currentCol = col, j = 0; j < colCount; currentCol++ , j++) {
            //                 sheet.getCell(currentRow, currentCol)
            //                     .text(options._oldCellText[currentRow][currentCol])
            //                     .backColor(options._oldBackColor[currentRow][currentCol])
            //                     .textIndent(options._oldTextIndent[currentRow][currentCol])
            //                     .backgroundImageLayout(options._oldBackgroundImageLayout[currentRow][currentCol])
            //                     .backgroundImage(options._oldBackGroundImage[currentRow][currentCol]);
            //             }
            //         }
            //     })
            // } else {
            //     const mainTable = `mainTable_${sheetIndex}`;
            //     options._oldBackColor = {};
            //     options._oldCellText = {};
            //     options._oldCellTypes = {};
            //     options._oldBackGroundImage = {};
            //     options._oldBackgroundImageLayout = {};
            //     options._oldTextIndent = {};
            //     options._oldTag = {};
            //     options._oldLocked = {};
            //     selections.forEach(selection => {
            //         const { row, col, rowCount, colCount } = selection;
            //         for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
            //             const currentRow = row + rowIndex;
            //             if (!options._oldCellText[currentRow]) {
            //                 options._oldCellText[currentRow] = {};
            //             }
            //             if (!options._oldBackColor[currentRow]) {
            //                 options._oldBackColor[currentRow] = {}
            //             }
            //             if (!options._oldCellTypes[currentRow]) {
            //                 options._oldCellTypes[currentRow] = {};
            //             }
            //             if (!options._oldTextIndent[currentRow]) {
            //                 options._oldTextIndent[currentRow] = {};
            //             }
            //             if (!options._oldBackGroundImage[currentRow]) {
            //                 options._oldBackGroundImage[currentRow] = {};
            //             }
            //             if (!options._oldBackgroundImageLayout[currentRow]) {
            //                 options._oldBackgroundImageLayout[currentRow] = {};
            //             }
            //             if (!options._oldLocked[currentRow]) {
            //                 options._oldLocked[currentRow] = {};
            //             }
            //             for (let colIndex = 0; colIndex < colCount; colIndex++) {
            //                 const currentCol = col + colIndex;
            //                 const cell = sheet.getCell(currentRow, currentCol);
            //                 options._oldBackColor[currentRow][currentCol] = cell.backColor();
            //                 options._oldCellText[currentRow][currentCol] = cell.text();
            //                 options._oldCellTypes[currentRow][currentCol] = cell.cellType();
            //                 options._oldTextIndent[currentRow][currentCol] = cell.textIndent();
            //                 options._oldBackGroundImage[currentRow][currentCol] = cell.backgroundImage();
            //                 options._oldBackgroundImageLayout[currentRow][currentCol] = cell.backgroundImageLayout();
            //                 options._oldLocked[currentRow][currentCol] = cell.locked();
            //             }
            //         }
            //         for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
            //             const currentRow = row + rowIndex;
            //             for (let colIndex = 0; colIndex < colCount; colIndex++) {
            //                 const currentCol = col + colIndex;
            //                 if (sheet.tables.find(currentRow, currentCol)) {
            //                     return;
            //                 }
            //             }
            //         }
            //         const detailIndex = getDetailIndex(mainTable, sheet);
            //         const detail = `detail_${detailIndex}`;
            //         const columnLabels = [];
            //         for (let index = 0; index < colCount; index++) {
            //             const fieldIndex = getFieldIndex(mainTable, detail);
            //             let label = sheet.getValue(row, col + index);
            //             if (!label) {
            //                 label = `字段${fieldIndex}`;
            //             }
            //             columnLabels.push({
            //                 fieldIndex,
            //                 label
            //             });
            //         }
            //         const table = sheet.tables.add(`表${detailIndex}`, row, col, rowCount, colCount, undefined, { showHeader: includeTitle === '1' });

            //         const detailFields = [];
            //         table.bindingPath(`${sheetName}.${detail}`);
            //         const { row: dataRow, col: dataCol, colCount: tableColCount } = table.dataRange();
            //         for (let index = 0; index < tableColCount; index++) {
            //             const { label, fieldIndex } = columnLabels[index]
            //             let fieldName = getPinyin(label)[0];
            //             if (!fieldName) {
            //                 fieldName = `field${fieldIndex}`;
            //             }
            //             fieldName = getFieldName(mainTable, detail, fieldName).toLowerCase();
            //             table.setColumnDataField(index, fieldName);
            //             table.setColumnName(index, label);

            //             detailFields.push({
            //                 col: dataCol + index,
            //                 colCount: 1,
            //                 fieldName,
            //                 fieldType: ['input', 'text', '200'],
            //                 label,
            //                 row: dataRow,
            //                 rowCount: 1,
            //                 detailIndex
            //             });
            //         }

            //         mainTableObj.detailTables.push({
            //             ...table.range(),
            //             detailIndex,
            //             fields: detailFields,
            //         });
            //         const columns = detailFields.map(field => (
            //             new GC.Spread.Sheets.Tables.TableColumn(field.fieldName).name(field.label).dataField(field.fieldName)
            //         ));
            //         table.bindColumns(columns);
            //         detailFields.forEach((detailField, detailFieldIndex) => {
            //             const fieldCellType = getFieldCellType(detailField);
            //             sheet.getCell(detailField.row, detailField.col)
            //                 .textIndent(2.5)
            //                 .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
            //                 .backgroundImage(fieldCellType.getFieldTypeImage())
            //                 .cellType(fieldCellType);

            //         });
            //     });
            // }
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        sheet.resumePaint();
    }
};